/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_mockservice
 * FILE       : MockLookupService.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 28, 2011  mycodetalks			       Created
 */
package net.lwf.lookup.fake.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.lwf.jaxb.service.JaxbService;
import net.lwf.lookup.fake.jaxb.Lookup;
import net.lwf.lookup.fake.jaxb.Lookups;

public class MockLookupService extends JaxbService {

	private static final String CONTEXT_PATH = "net.lwf.lookup.fake.jaxb";
	private String xmlFileName = "lookups.xml";

	private static Map<String, Lookup> lookups;
	private static Map<String, List<Lookup>> lookupData;
	
	public String getLookupValue(String group, String key) {
		
		if(lookups == null) {
			this.init();
		}
		if(lookups.containsKey(group+key)) {
			Lookup lookup = lookups.get(group+key);
			return lookup.getValue();
		}
		return null;
	}
	
	public List<Lookup> getData(String group) {
		if(lookupData == null) {
			this.init();
		}
		return lookupData.get(group);
	}
	
	@Override
	public String getContextPath() {
		return CONTEXT_PATH;
	}

	@Override
	public String getXmlFileName() {
		return xmlFileName;
	}
	
	private void init() {
		lookups = new HashMap<String, Lookup>();
		Lookups lookupdata = (Lookups) loadedXmlData();
		for(Lookup lookup: lookupdata.getLookup()) {
			lookups.put(lookup.getGroup()+lookup.getKey(), lookup);
			this.processGroup(lookup);
		}
	}
	
	private void processGroup(Lookup lookup) {
		if(lookupData == null) {
			lookupData = new HashMap<String, List<Lookup>>();
		}
		List<Lookup> data = null;
		if(lookupData.containsKey(lookup.getGroup())) {
			data = lookupData.get(lookup.getGroup());
		}
		else {
			data = new ArrayList<Lookup>();
		}
		data.add(lookup);
		lookupData.put(lookup.getGroup(), data);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MockLookupService service = new MockLookupService();
		String value = service.getLookupValue("SUBJECTS", "english");
		System.out.println(value);

	}

}
